package com.dy.controller;

import com.dy.pojo.dto.DyMessageDTO;
import com.dy.pojo.vo.DyMessageVO;
import com.dy.service.DyMessageService;
import com.dy.utils.ApiResousUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)
 * @Date: 2021/6/9 16:46
 * @Version 1.0 (版本)
 */
@RestController
@RequestMapping("/message")
public class DyMessageController {
    @Autowired
    private DyMessageService dyMessageService;

    /**
     * 查询
     * @return
     */
    @GetMapping("/query")
    public ApiResousUtil<List<DyMessageVO>> query(){
        List<DyMessageVO> list= dyMessageService.selectAll();
        return ApiResousUtil.ok(list);
    }

    /**
     * 添加留言/及回复留言
     * @param dyMessageDTO
     * @return ApiResousUtil
     */
    @PostMapping("/create")
    public ApiResousUtil<String> create(@RequestBody DyMessageDTO dyMessageDTO, HttpSession session){
        String userCode = (String) session.getAttribute("code");
        if (userCode == null) {
            return ApiResousUtil.error("请先登录");
        }
        if (dyMessageDTO.getParentCode() == null) {
            dyMessageService.create(dyMessageDTO,userCode);
            return ApiResousUtil.ok("留言成功");
        }
        Boolean f = dyMessageService.replyMessage(dyMessageDTO, userCode);
        if (f == false) {
            return ApiResousUtil.error("没有父code");
        }
        return ApiResousUtil.ok("回复成功");
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public ApiResousUtil<String> delete(@RequestParam("id")Long id){
        if (id == null) {
            return ApiResousUtil.error("id为空");
        }
        dyMessageService.delete(id);
        return ApiResousUtil.ok("删除成功");
    }

}
